<?php
namespace app\decorate\controller;
use app\admin\controller\Admin;
use app\AdminRest;
use app\ApiRest;
use think\App;
use think\facade\Db;
use think\Request;
use app\decorate\model\IndexSite as site_model;
use app\decorate\model\AdminProgress;
use app\decorate\model\AdminStyle;
use app\decorate\model\AdminArea;
use app\decorate\model\AdminHousewear;
use app\decorate\model\AdminPrice;
use app\decorate\model\AdminMeasure;
use app\decorate\model\IndexSiteIv;
use app\decorate\model\AdminDynamic;
use app\decorate\model\IndexCollect;

class IndexSite extends ApiRest{
    protected $model;
    protected $progress_model;
    protected $style_model;
    protected $area_model;
    protected $house_wear_model;
    protected $price_model;
    protected $m_model;
    protected $iv_model;
    protected $dyn_model;
    protected $collect_model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model           = new site_model();
        $this->progress_model  = new AdminProgress();
        $this->style_model     = new AdminStyle();
        $this->area_model      = new AdminArea();
        $this->house_wear_model= new AdminHousewear();
        $this->price_model     = new AdminPrice();
        $this->m_model         = new AdminMeasure();
        $this->iv_model        = new IndexSiteIv();
        $this->dyn_model       = new AdminDynamic();
        $this->collect_model   = new IndexCollect();
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:58
     * @return \think\Response
     * descption:工地列表
     */
    public function siteList(){
        $input = $this->_input;
        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['status' ,'=',1];
        if(!empty($input['name'])){
            $dis[] = ['title','like',"%\\".$input['name']."%"];
        }
        if(!empty($input['housewear'])){
            $input['housewear'] = array_column($input['housewear'],'id');
            $dis[] = ['housewear','in',$input['housewear']];
        }
        if(!empty($input['progress'])){
            $input['progress'] = array_column($input['progress'],'id');
            $dis[] = ['progress','in',$input['progress']];
        }
        if(!empty($input['area'])){

            $area = $this->area_model->resetV2([$input['area']]);

            $dis[] = ['area','in',$area];
        }
        $dis_price = array();
        if(!empty($input['price'])){
            foreach ($input['price'] as $value){
                $min = $value['min'];
                $max = $value['max'];
                $dis_price[] = ['total_price','between',"$min,$max"];
            }
        }
        $dis_m = array();
        if(!empty($input['m'])){
            foreach ($input['m'] as $value){
                $min = $value['min'];
                $max = $value['max'];
                $dis_m[] = ['m','between',"$min,$max"];
            }
        }
        if($input['sort']==3){
            $lat = !empty($input['latitude'])?$input['latitude']:0;
            $lng = !empty($input['longitude'])?$input['longitude']:0;
            $alh = '(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*('.$lng.'- `long`)/360),2)+COS(PI()*33.07078170776367/180)* COS('.$lat.' * PI()/180)*POW(SIN(PI()*('.$lat.'- latitude)/360),2)))) as distance';
        }else{
            $alh = 'sort';
        }
        $data= $this->model->siteList($dis,10,$dis_price,$dis_m,$input['sort'],$alh);
        return $this->success($data);
    }



    /**
     * User: chenniang
     * Date: 2019-10-17 17:14
     * @return void
     * descption:工地首页
     */
    public function siteIndex(){
        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['status' ,'=',1];
        $data['site']     = $this->model->siteList($dis);
        $data['area']     = $this->area_model->qareaSelect($dis);
        $data['progress'] = $this->progress_model->progressSelect($dis);
        $data['housewear']= $this->house_wear_model->housewearSelect($dis);
        $data['price']    = $this->price_model->priceSelect($dis);
        $data['measure']  = $this->m_model->mselect($dis);
        return $this->success($data);
    }
    /**
     * User: chenniang
     * Date: 2019-10-17 17:55
     * @return void
     * descption:工地详情
     */
    public function siteInfo(){
        $input = $this->_param;
        $data['iv']   = $this->updateIv($input);
        $this->model->where(['id'=>$input['id']])->update(['iv'=>Db::raw('iv+1')]);
        $data['info'] = $this->model->siteInfo(['id'=>$input['id']]);
        if(empty($data['info'])){
            $this->collect_model->collectDel($this->getUserId(),$input['id'],2);
            return $this->error('del data');
        }
        $collect      = $this->collect_model->collectInfo(['user_id'=>$this->getUserId(),'type'=>2,'news_id'=>$data['info']['id'],'status'=>1]);
        $data['info']['is_collect'] = !empty($collect)?1:0;
        $data['info']['iv_ing']     = $this->ivIng($data['info']['iv']);
        $data['dyn']      = $this->dyn_model->dynList(['a.uniacid'=>$this->_uniacid,'a.site_id'=>$input['id'],'a.status'=>1]);
        $data['progress'] = $this->progress_model->progressSelect(['uniacid'=>$this->_uniacid]);
        if(!empty($data['progress'])){
            foreach ($data['progress'] as &$v){
                $v['ing'] = $v['id'] ==$data['info']['progress']?1:0;
            }
        }
        $data['staff_phone'] = Db::name('longbing_card_user_info')->where(['fans_id'=>$input['staff_id']])->value('phone');

        $site = lbPulgSettingInfo($this->_uniacid,'decorate',['offer_switch']);

        $data['offer_switch'] = $site['offer_switch'];

        return $this->success($data);
    }


    /**
     * User: chenniang
     * Date: 2019-10-23 15:23
     * @param $iv
     * @return int
     * descption:正在浏览人数
     */
    public function ivIng($iv){
        $data = intval($iv/10);
        if($iv>10){
            $data = rand($data-intval($data/10),$data+intval($data/10));
        }else{
            $data = $iv;
        }
        return $data;
    }

    /**
     * User: chenniang
     * Date: 2019-10-17 19:13
     * @return \think\Response
     * descption:动态列表
     */
    public function dynList(){
        $input = $this->_param;
        $data  = $this->dyn_model->dynList(['a.uniacid'=>$this->_uniacid,'a.site_id'=>$input['id'],'a.status'=>1]);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-10-17 19:15
     * @return void
     * descption:工地成员
     */

    public function siteUser(){
        $input = $this->_param;
        $data  = $this->model->siteInfo(['id'=>$input['id']]);
        $staff = !empty($data['designer'])?explode(',',$data['designer']):[];
        $dis[] = ['a.fans_id','in',$staff];
        $dis[] = ['a.is_staff','=',1];
        $user  = Db::name('longbing_card_user_info')
            ->alias('a')
            ->leftJoin('longbing_card_job b','b.id = a.job_id')
            ->where($dis)
            ->field("a.name,a.avatar,a.fans_id,b.name as job_name")
            ->group('a.id')
            ->paginate(10)
            ->toArray();
        if(!empty($user['data'])){
            foreach ($user['data'] as &$value){
                if(empty($value['avatar'])){
                    $value['avatar'] = Db::name('longbing_card_user')->where(['id'=>$value['fans_id']])->value('avatarUrl');
                    $value['avatar'] = !empty($value['avatar'])?$value['avatar']:'https://lbqnyv2.migugu.com/defaultAvatar.png';
                }
                if(empty($value['name'])){
                    $value['name'] = Db::name('longbing_card_user')->where(['id'=>$value['fans_id']])->value('nickName');
                }
                $value['job_name'] = !empty($value['job_name'])?$value['job_name']:'';
            }
        }
        $user['data'] = transImagesOne($user['data'],['avatar']);
        return $this->success($user);
    }

    /**
     * User: chenniang
     * Date: 2019-10-17 18:34
     * @return void
     * descption:检查更新浏览记录
     */
    public function updateIv($input){
        $ins['site_id'] = $input['id'];
        $ins['user_id'] = $this->getUserId();
        $ins['uniacid'] = $this->_uniacid;
//        $ins['staff_id']= $input['staff_id'];
        $iv = $this->iv_model->ivInfo($ins);
        if(empty($iv)){
            $this->iv_model->ivAdd($ins);
        }else{
            $this->iv_model->ivUpdate($ins,['create_time'=>time()]);
        }
        $data = $this->iv_model->ivSelect(['a.site_id'=>$input['id'],'a.uniacid'=>$this->_uniacid]);
        return $data;
    }

    /**
     * User: chenniang
     * Date: 2019-10-23 10:16
     * @return void
     * descption:员工工地
     */
    public function staffSite(){
        $input = $this->_param;
        $dis[] = ['a.uniacid','=',$this->_uniacid];
        $dis[] = ['a.status' ,'=',1];
        $dis[] = ['b.staff_id','=',$input['staff_id']];
        $data  = $this->model->staffSiteList($dis);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-10-23 14:11
     * @return void
     * descption:用户工地
     */
    public function userSite(){
        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['status' ,'>',-1];
        $dis[] = ['owner','=',$this->getUserId()];
        $data  = $this->model->siteList($dis);
        return $this->success($data);
    }


    public function diylist()
    {
        $param = $this->_param;
        $filter[] = ['uniacid', '=', $this->_uniacid];
        $filter[] = ['status' , '=', 1];
        if (!empty($param['keyword'])) {
            $filter[] = ['title', 'like', '%' . $param['keyword'] . '%' ];
        }
        $list = $this->model->where($filter)
            ->paginate(['list_rows' => $param['page_count'] ? $param['page_count'] : 10, 'page' => $param['page'] ? $param['page'] : 1])->toArray();

        return $this->success($list);
    }
}
